import { defineStore } from 'pinia'
import { useAppStore } from './app'
import { getMarkets } from '@/api/betting'
import { SportsType, BettingType } from '@/enums'
import { MarketMainType } from '@/types'

export const useMarketStore = defineStore('market', {
	state: () => ({
		markets: [] as MarketMainType[],
		filters: {} as { [key: string]: BettingType[] }
	}),
	actions: {
		async getMarkets() {
			const appStore = useAppStore()
			try {
				const { data } = await getMarkets({
					locale: appStore.lang,
					oddsStyle: appStore.odds,
					location: appStore.region
				})
				this.markets = data.recommends
			} catch {}
		},
		setFilters(sportId: SportsType, list: BettingType[]) {
			this.filters[sportId] = list
		}
	},
	getters: {
		mainMarkets:
			(state) =>
			(sportId: SportsType, isLive = false) =>
				state.markets.filter(
					(item) => item.sportId === sportId && item.type === (isLive ? 'live' : 'upcoming')
				)
		// liveMarketTypeId: (state) => (sportId, marketTypeId) => {
		// 	const upcomingMarket = state.markets.find(
		// 		(item) =>
		// 			item.sportId === sportId && item.marketTypeId === marketTypeId && item.type === 'upcoming'
		// 	)
		// 	if (upcomingMarket) {
		// 		const liveMarket = state.markets.find(
		// 			(item) =>
		// 				item.sportId === sportId &&
		// 				item.marketTypeCate === upcomingMarket.marketTypeCate &&
		// 				item.type === 'live'
		// 		)
		// 		return liveMarket?.marketTypeId
		// 	}
		// 	return null
		// }
	},
	persist: true
})
